<template>
  <div class="pagination_comp">
    <el-pagination
      v-model:current-page="localCurrentPage"
      :page-size="pageSize"
      :total="total"
      :page-sizes="[10, 20, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script setup>
import { defineProps, defineEmits, ref, watch } from 'vue'

const props = defineProps({
  currentPage: {
    type: Number,
    default: 1,
  },
  pageSize: {
    type: Number,
    default: 10,
  },
  total: {
    type: Number,
    default: 0,
  },
})

const emits = defineEmits(['update:currentPage', 'update:pageSize', 'pageChange'])

const localCurrentPage = ref(props.currentPage)

// 监听父组件 currentPage 变化，同步到 localCurrentPage
watch(
  () => props.currentPage,
  (val) => {
    localCurrentPage.value = val
  }
)

// 当前页改变
const handleCurrentChange = (val) => {
  emits('update:currentPage', val)
  emits('pageChange', { currentPage: val, pageSize: props.pageSize })
}

// 每页条数改变
const handleSizeChange = (val) => {
  emits('update:pageSize', val)
  emits('pageChange', { currentPage: localCurrentPage.value, pageSize: val })
}
</script>

<style scoped>
.pagination_comp {
  margin: 20px 0;
  display: flex;
  justify-content: center;
}
</style>
